import api from '../../../utils/api';
Page({
	/**
   * 页面的初始数据
   */
	data: {
		age:'',
		ossHost: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/healthRecords/bag3.jpg',
		ossHostTop: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/healthRecords/bbg.png',
		show: false,
		ll: [
			{
				name: '体质',
				code: 'lk1590141731679',
				icon: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/healthRecords/icon-headDirection.png',
				value: '',
				coordinate: 'left: 150rpx;right: 30rpx;top: 120rpx;',
				url:'/facetongue/pages/home/index'
			},
			{
				name: '步数',
				code: 'lk1591234989246',
				icon: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/healthRecords/bushu.png',
				value: '',
				coordinate: 'left: 80rpx;right: 30rpx;top: 240rpx;',
				url:'/sportsAdministration/pages/hometab/index'
			},
			{
				name: '睡眠',
				code: 'XYZBXY001041',
				icon: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/healthRecords/shuimianshichang%402x.png',
				value: '',
				coordinate: 'left: 40rpx; top: 360rpx;',
				url:'/sleepManagement/pages/home/index'
			},
			{
				name: '体重',
				code: 'XYZBXY001007',
				icon: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/healthRecords/icontz.png',
				value: '',
				coordinate: 'left: 20rpx;top: 500rpx;',
				url:'/weightManagement/pages/home/index'
			},
			{
				name: 'BMI',
				code: 'XYZBXY001001',
				icon: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/healthRecords/BMI.png',
				value: '',
				coordinate: 'left: 40rpx;top: 650rpx;',
				url:'/weightManagement/pages/home/index'
			},
			{
				name: '心电',
				code: '',
				icon: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/healthRecords/xindianjianhuchuchanyi.png',
				value: '',
				coordinate: 'left: 60rpx;top: 800rpx;'
			},
			{
				name: '尿酸',
				code: 'lk1681869139108',
				icon: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/healthRecords/jianyanke.png',
				value: '',
				coordinate: 'left: 110rpx;top: 960rpx;',
				url:'/three-in-one/pages/home/index'
			},
			{
				name: '舒张压',
				code: 'XYZBXY001002',
				icon: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/healthRecords/huabanfuben.png',
				value: '',
				coordinate: 'right: 150rpx;top: 120rpx;',
				url:'/blood/pages/home/index'
			},
			{
				name: '收缩压',
				code: 'XYZBXY001003',
				icon: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/healthRecords/huabanfuben.png',
				value: '',
				coordinate: 'right: 65rpx;top: 240rpx;',
				url:'/blood/pages/home/index'
			},
			{
				name: '心率',
				code: 'XYZBXY001005',
				icon: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/healthRecords/BasicIcons-all-.png',
				value: '',
				coordinate: 'right: 50rpx;top: 370rpx;',
				url:'/blood/pages/home/index'
			},
			{
				name: '血糖',
				code: 'XYZBXY001008',
				icon: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/healthRecords/gaoxietang-%402x.png',
				value: '',
				coordinate: 'right: 20rpx;top: 500rpx;',
				url:'/three-in-one/pages/home/index'
			},
			{
				name: '血氧',
				code: 'XYZBXY001004',
				icon: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/healthRecords/ziyuan-3.png',
				value: '',
				coordinate: 'right: 30rpx;top: 650rpx;',
				url:'/bloodoxygen/pages/home/index'
			},
			{
				name: '呼吸',
				code: 'lk1681869139062',
				icon: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/healthRecords/huxike.png',
				value: '',
				coordinate: 'right: 50rpx;top: 800rpx;',
				url:'/breath/pages/home/index'
			},
			{
				name: '血酮',
				signindays: '',
				code: 'lk1681869139105',
				liveness: '',
				icon: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/healthRecords/ziyuan-4.png',
				value: '',
				coordinate: 'right: 100rpx;top: 960rpx;',
				url:'/three-in-one/pages/home/index'
			}
		],
		list: null,
		isGender: false, //性别显隐
		startT: '',
		birthday: ''
	},

	/**
   * 生命周期函数--监听页面加载
   */
	onLoad(options) {
		this.getlist();
		this.gettizhi()
	},
	godaka() {
		wx.navigateTo({
			url: `/dailyAttendance/pages/home/index`,
		  })
	},
	// 根据出生日期获取周岁年龄
    getAge (strBirthday) {
        let returnAge
        let strBirthdayArr = strBirthday.split('-')
        let birthYear = strBirthdayArr[0]
        let birthMonth = strBirthdayArr[1]
        let birthDay = strBirthdayArr[2]
        let d = new Date()
        let nowYear = d.getFullYear()
        let nowMonth = d.getMonth() + 1
        let nowDay = d.getDate()
        if (nowYear == birthYear) {
            returnAge = 0 //同年则为0岁
        } else {
            let ageDiff = nowYear - birthYear //年之差
            if (ageDiff > 0) {
                if (nowMonth == birthMonth) {
                    let dayDiff = nowDay - birthDay //日之差
                    if (dayDiff < 0) {
                        returnAge = ageDiff - 1
                    } else {
                        returnAge = ageDiff
                    }
                } else {
                    let monthDiff = nowMonth - birthMonth //月之差
                    if (monthDiff < 0) {
                        returnAge = ageDiff - 1
                    } else {
                        returnAge = ageDiff
                    }
                }
            } else {
                returnAge = -1 //返回-1 表示出生日期输入错误 晚于今天
            }
        }
        return returnAge // 返回周岁年龄
    },
	getlist() {
		// 整体列表查询
		try {
			api.post('/api/hxhealth/user/load').then((res) => {
				// console.log(res);
				console.log('----res----', res[0]);

				if (res[0].birthday) {
					let userInfo = wx.getStorageSync('userInfo');
					console.log(res[0].birthday);
					console.log(this.getAge(res[0].birthday));
					res[0].age = this.getAge(res[0].birthday)
				}
				if (!res[0].gender || res[0].gender == '') {
					let userInfo = wx.getStorageSync('userInfo');
				}
				if (res[0].height && res[0].weight) {
					let height = parseInt(res[0].height) / 100 || 0;
					res[0].bmi = (Number(res[0].weight) / (height * height)).toFixed(0);
					res[0].height = Number(res[0].height).toFixed(0);
					res[0].weight = Number(res[0].weight).toFixed(0);
				}
				console.log(res[0]);
				this.setData({
					list: res[0],
					birthday: res[0].birthday,
					startT: res[0].birthday
				});
			});
		} catch (e) {
			console.error(e);
		}
	},
	 getlistkind(tizhi = '') {
		try {
			api
				.post('/api/exams/lastvalues', {
					data: {
						unionuser: true
					}
				})
				.then((res) => {
					// console.log(res);
					console.log('----res----', res[0]);
					let array = this.data.ll;
					for (let i = 0; i < res.length; i++) {
						for (let g = 0; g < array.length; g++) {
							if (res[i].code == array[g].code) {
								if(res[i].code == 'XYZBXY001007'|| res[i].code == 'XYZBXY001001'|| res[i].code == 'XYZBXY001008') {
									console.log(res[i].value);
									array[g].value = parseFloat(res[i].value).toFixed(1)
								} else if(res[i].code == 'lk1681869139062') {
									console.log(res[i].value);
									array[g].value = parseFloat(res[i].value).toFixed(2)
								} else if(res[i].code == 'XYZBXY001041'){
									console.log(res[i].value);
									res[i].value = parseFloat(res[i].value)
									if(90>=res[i].value<=100) {
										res[i].value = '好极了'
									}
									if(80>=res[i].value<=80) {
										res[i].value = '不错'
									}
									if(60>=res[i].value<=80) {
										res[i].value = '一般'
									} else {
										res[i].value = '太差了'
									}
									array[g].value = res[i].value
								} else if (res[i].code == 'lk1590141731679'){
									console.log(tizhi);
									array[g].value = tizhi
									  console.log(this.data.tizhi);
								} else {
									array[g].value = parseFloat(res[i].value).toFixed(0)
								}

							}
						}
					}
					console.log(array);
					this.setData({
						ll: array,
						show: true
					});
				});
		} catch (e) {
			console.error(e);
		}
	},
	async gettizhi() {
		await api.post('/api/hxhealth/exams/records', {
			data: {
				everyday: false,
				devkind: ""
			}
		}).then((res) => {
			console.log('------2222-----');
			console.log(res);
			if(res.length>0) {
				let tizhi = JSON.parse(res[0].summary).diseaseName;
				console.log(tizhi);
				this.getlistkind(tizhi);
			}
		});
	},
	goPageUrl(e) {
		console.log(e.currentTarget.dataset.url);
		let url = e.currentTarget.dataset.url
		wx.navigateTo({ url: url });
	},
	/**
   * 生命周期函数--监听页面初次渲染完成
   */
	onReady() {},

	/**
   * 生命周期函数--监听页面显示
   */
	onShow() {},

	/**
   * 生命周期函数--监听页面隐藏
   */
	onHide() {},

	/**
   * 生命周期函数--监听页面卸载
   */
	onUnload() {},

	/**
   * 页面相关事件处理函数--监听用户下拉动作
   */
	onPullDownRefresh() {},

	/**
   * 页面上拉触底事件的处理函数
   */
	onReachBottom() {},

	/**
   * 用户点击右上角分享
   */
	onShareAppMessage() {}
});
